package com.nota.flink.cdc.utils;

import cn.hutool.core.util.StrUtil;
import com.nota.flink.cdc.common.config.MysqlCdcInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 胡超雄
 * @date 2024/01/08 10:50
 * @desc
 */
@Component
public class MysqlUtil {


    @Resource
    private MysqlCdcInfo mysqlCdcInfo;


    public List<String> getAllTable() {
        List<String> allTable = new ArrayList<>();
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册 JDBC 驱动
            Class.forName(mysqlCdcInfo.getDriverClassName());

            // 打开链接
            // System.out.println("连接数据库...");
            conn = DriverManager.getConnection(mysqlCdcInfo.getUrl(), mysqlCdcInfo.getUser(), mysqlCdcInfo.getPwd());

            // 执行查询
            // System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "show tables";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                String tableName = rs.getString(1); // 从第一个字段获取表名
                if ("system_role_auth".equals(tableName)) {
                    continue;
                }
                allTable.add(tableName);
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return allTable;
    }

    /**
     * 查询主键
     *
     * @param tableName 表名
     * @return
     */
    public String getPrimary(String tableName) {
        String primary = StrUtil.EMPTY;
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册 JDBC 驱动
            Class.forName(mysqlCdcInfo.getDriverClassName());

            // 打开链接
            //System.out.println("连接数据库...");
            conn = DriverManager.getConnection(mysqlCdcInfo.getUrl(), mysqlCdcInfo.getUser(), mysqlCdcInfo.getPwd());

            // 执行查询
            //System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SHOW KEYS FROM " + tableName + " WHERE Key_name = 'PRIMARY'";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                primary = rs.getString("Column_name");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return primary;
    }

    /**
     * 建表语句
     *
     * @param tableName
     * @return
     */
    public String showCreateTable(String tableName) {
        String primary = StrUtil.EMPTY;
        Connection conn = null;
        Statement stmt = null;
        try {
            // 注册 JDBC 驱动
            Class.forName(mysqlCdcInfo.getDriverClassName());

            // 打开链接
            //System.out.println("连接数据库...");
            conn = DriverManager.getConnection(mysqlCdcInfo.getUrl(), mysqlCdcInfo.getUser(), mysqlCdcInfo.getPwd());

            // 执行查询
            //System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SHOW CREATE TABLE " + tableName;
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                primary = rs.getString("Create Table");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        return primary;
    }

}
